package com.iot.inclinometer.dao.impl;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.iot.inclinometer.dao.IDataInfoDao;
import com.iot.inclinometer.db.DBHelper;
import com.iot.inclinometer.pojo.DataInfo;

import java.util.ArrayList;
import java.util.List;

public class DataInfoDaoImpl implements IDataInfoDao {
	private DBHelper helper;

	public DataInfoDaoImpl(Context context) {
		helper = new DBHelper(context);
	}
	
	@Override
	public boolean insertAll(List<DataInfo> allDatas) {
		boolean flag = false;
		SQLiteDatabase database = helper.getWritableDatabase();
		database.beginTransaction();// 开始事务
		String sql = "INSERT INTO data_info(depth,lx,ly,error,theta_x,theta_y,theta,ob_id) VALUES (?,?,?,?,?,?,?,?);";
		try {
			// 循环将数据存入数据库中
			for (DataInfo dataInfo : allDatas) {
				database.execSQL(sql, new Object[] { dataInfo.getDepth(),
						dataInfo.getLx(),dataInfo.getLy(),dataInfo.getError(),dataInfo.getThetaX(),dataInfo.getThetaY(),
						dataInfo.getTheta(),dataInfo.getObserverId()});
			}
			database.setTransactionSuccessful();
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			database.endTransaction();
			if (database != null) {
				database.close();
			}
		}
		return flag;
	}

	@Override
	public List<DataInfo> selectDataInfos() {
		List<DataInfo> allDataInfos = new ArrayList<DataInfo>();
		SQLiteDatabase database = null;
		
		try {
			String sql = "select * from data_info";
			database = helper.getReadableDatabase();
			Cursor cursor = database.rawQuery(sql,null);
			while (cursor.moveToNext()) {
				int _id = cursor.getInt(cursor.getColumnIndex("data_id"));
				int _obId = cursor.getInt(cursor.getColumnIndex("ob_id"));
				String _depth = cursor
						.getString(cursor.getColumnIndex("depth"));
				String _lx = cursor
						.getString(cursor.getColumnIndex("lx"));
				String _ly = cursor
						.getString(cursor.getColumnIndex("ly"));
				String _error = cursor
						.getString(cursor.getColumnIndex("error"));
				String _thetaX = cursor
						.getString(cursor.getColumnIndex("theta_x"));
				String _thetaY = cursor
						.getString(cursor.getColumnIndex("theta_y"));
				String _theta = cursor
						.getString(cursor.getColumnIndex("theta"));
				
				DataInfo info = new DataInfo();
				info.setDataId(_id);
				info.setObserverId(_obId);
				info.setDepth(_depth);
				info.setLx(_lx);
				info.setLy(_ly);
				info.setError(_error);
				info.setThetaX(_thetaX);
				info.setThetaY(_thetaY);
				info.setTheta(_theta);
				allDataInfos.add(info);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (database != null) {
				database.close();
			}
		}
		return allDataInfos;
	}
	
	@Override
	public List<DataInfo> selectDataInfos(int obId) {

		List<DataInfo> allDataInfos = new ArrayList<DataInfo>();

		SQLiteDatabase database = helper.getReadableDatabase();
		String sql = "select * from data_info where ob_id = ?";
		try {
			Cursor cursor = database.rawQuery(sql,
					new String[] { Integer.toString(obId) });

			while (cursor.moveToNext()) {

				int _id = cursor.getInt(cursor.getColumnIndex("data_id"));
				int _obId = cursor.getInt(cursor.getColumnIndex("ob_id"));
				String _depth = cursor
						.getString(cursor.getColumnIndex("depth"));
				String _lx = cursor
						.getString(cursor.getColumnIndex("lx"));
				String _ly = cursor
						.getString(cursor.getColumnIndex("ly"));
				String _error = cursor
						.getString(cursor.getColumnIndex("error"));
				String _thetaX = cursor
						.getString(cursor.getColumnIndex("theta_x"));
				String _thetaY = cursor
						.getString(cursor.getColumnIndex("theta_y"));
				String _theta = cursor
						.getString(cursor.getColumnIndex("theta"));
				
				DataInfo info = new DataInfo();
				info.setDataId(_id);
				info.setObserverId(_obId);
				info.setDepth(_depth);
				info.setLx(_lx);
				info.setLy(_ly);
				info.setError(_error);
				info.setThetaX(_thetaX);
				info.setThetaY(_thetaY);
				info.setTheta(_theta);
				allDataInfos.add(info);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (database != null) {
				database.close();
			}
		}
		return allDataInfos;
	}
	
	/**
	 * 通过obId删除测量结果数据
	 */
	@Override
	public int deleteDataInfosByobId(int obId){
		//得到可写的SQLiteDatabase对象  
        SQLiteDatabase database = helper.getWritableDatabase();  
        //调用delete方法，删除数据  
        int flag=database.delete("data_info", "ob_id=?", new String[]{Integer.toString(obId)});  
        return flag;
	}

	@Override
	public int deleteAllDataInfos() {
		//得到可写的SQLiteDatabase对象  
        SQLiteDatabase database = helper.getWritableDatabase();  
        String sql = "delete from data_info";
        database.execSQL(sql);
        database.close();
        //调用delete方法，删除数据  
        int flag=1;
        return flag;
	}
}
